package com.jihui.jihuipicturebackend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jihui.jihuipicturebackend.annotation.CheckAuth;
import com.jihui.jihuipicturebackend.common.BaseResponse;
import com.jihui.jihuipicturebackend.common.ResultUtils;
import com.jihui.jihuipicturebackend.constant.UserConstant;
import com.jihui.jihuipicturebackend.exception.BusinessException;
import com.jihui.jihuipicturebackend.exception.ErrorCode;
import com.jihui.jihuipicturebackend.exception.ThrowUtils;
import com.jihui.jihuipicturebackend.model.dto.picture.PictureUploadRequest;
import com.jihui.jihuipicturebackend.model.dto.user.*;
import com.jihui.jihuipicturebackend.model.entity.User;
import com.jihui.jihuipicturebackend.model.vo.PictureVO;
import com.jihui.jihuipicturebackend.model.vo.UserLoginVO;
import com.jihui.jihuipicturebackend.model.vo.UserVO;
import com.jihui.jihuipicturebackend.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author jihui
 * @date 2024/12/24 23:52
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户模块")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 用户注册
     * @param request
     * @return
     */
    @PostMapping("/register")
    @ApiOperation( value = "用户注册接口")
    public BaseResponse<Long> register(@RequestBody UserRegisterRequest request){
        ThrowUtils.throwIf(request == null, ErrorCode.NOT_FOUND_ERROR);
        String userAccount = request.getUserAccount();
        String userPassword = request.getUserPassword();
        String checkPassword = request.getCheckPassword();
        Long registerId = userService.register(userAccount, userPassword, checkPassword);
        return ResultUtils.success(registerId);
    }

    /**
     * 用户登录
     * @param request 登录请求
     * @return 用户登录信息
     */
    @PostMapping("/login")
    @ApiOperation( value = "用户登录接口")
    public BaseResponse<UserLoginVO> register(@RequestBody UserLoginRequest request, HttpServletRequest httpServletRequest){
        ThrowUtils.throwIf(request == null, ErrorCode.NOT_FOUND_ERROR);
        String userAccount = request.getUserAccount();
        String userPassword = request.getUserPassword();
        UserLoginVO userLoginVO = userService.userLogin(userAccount, userPassword, httpServletRequest);
        return ResultUtils.success(userLoginVO);
    }

    /**
     * 用户登录状态检查
     * @return
     */
    @GetMapping("/checkLogin")
    @ApiOperation( value = "用户登录状态检查接口")
    public BaseResponse<UserLoginVO> UserCheckLogin(HttpServletRequest httpServletRequest){
        User user = userService.getUserLoginVOByRequest(httpServletRequest);
        ThrowUtils.throwIf(user == null, ErrorCode.NOT_LOGIN_ERROR);
        UserLoginVO userLoginVO = userService.getUserLoginVO(user);
        return ResultUtils.success(userLoginVO);
    }

    /**
     * 用户退出登录
     * @param request 请求
     * @return 退出登录结果
     */
    @PostMapping("/logout")
    @ApiOperation( value = "用户退出登录接口")
    public BaseResponse<Boolean> userLogout(HttpServletRequest request) {
        ThrowUtils.throwIf(request == null, ErrorCode.PARAMS_ERROR);
        boolean result = userService.userLoginOut(request);
        return ResultUtils.success(result);
    }


    /**
     * 获取用户信息
     * @param userQueryRequest 请求参数
     * @return 用户信息
     */
    @PostMapping("/page/getUser")
    @ApiOperation( value = "管理员获取用户信息接口")
    @CheckAuth(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<UserVO>> getUserInfo(@RequestBody UserQueryRequest userQueryRequest){
        // 参数校验
        ThrowUtils.throwIf(userQueryRequest == null, ErrorCode.PARAMS_ERROR);
        int current = userQueryRequest.getCurrent();
        int pageSize = userQueryRequest.getPageSize();
        // 分页查询
        Page<User> pageUser = userService.page(new Page(current, pageSize),
                userService.getUserQueryWrapper(userQueryRequest));
        // 转换成VO
        Page<UserVO> pageUserVO = new Page<>(current, pageSize, pageUser.getTotal());
        List<UserVO> userVOList = userService.getUserVOByQuery(pageUser.getRecords());
        pageUserVO.setRecords(userVOList);
        return ResultUtils.success(pageUserVO);

    }

    /**
     * 添加用户
     * @param userAddRequest
     * @return
     */
    @PostMapping("/addUser")
    @ApiOperation( value = "管理员添加用户接口")
    @CheckAuth(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> addUser(@RequestBody UserAddRequest userAddRequest) {
        // 参数校验
        ThrowUtils.throwIf(userAddRequest == null, ErrorCode.PARAMS_ERROR);
        userService.addUser(userAddRequest);
        return ResultUtils.success(true);
    }

    /**
     * 更新用户信息
     * @param userUpdateRequest
     * @return
     */
    @PostMapping("/updateUser")
    @ApiOperation( value = "管理员更新用户信息接口")
    @CheckAuth(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateUser(@RequestBody UserUpdateRequest userUpdateRequest) {
        ThrowUtils.throwIf(userUpdateRequest == null, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(userService.updateUser(userUpdateRequest));
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @DeleteMapping("/deleteUser/id")
    @ApiOperation( value = "管理员删除用户接口")
    @CheckAuth(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteUser(@RequestParam("id") Long id) {
        ThrowUtils.throwIf(id == null, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(userService.deleteUser(id));
    }


    @PostMapping("/edit")
    @CheckAuth(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation(value = "用户信息编辑")
    public BaseResponse<UserVO> editUser(@RequestPart("file") MultipartFile multipartFile,
                                                 UserUpdateRequest userUpdateRequest) {
        UserVO userVO = userService.editUser(multipartFile, userUpdateRequest);
        ThrowUtils.throwIf(userVO == null,
                new BusinessException(ErrorCode.OPERATION_ERROR, "用户信息编辑失败"));
        return ResultUtils.success(userVO);
    }
}

